// --------------------------------------------------------------------------------------------------------------------
// <copyright file="KukaLBR4Tests.cs" company="Microsoft Corporation">
// The MIT License (MIT)
// 
// Copyright (c) 2014, Microsoft Corporation
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Test.Robotics.Hardware.KukaLBR.KukaLBR4Tests
{
    using System;
    using System.Runtime.InteropServices;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Software only unit tests for KukaLBR4 specific components
    /// </summary>
    [DeploymentItem("Microsoft.Robotics.Hardware.KukaLBR.KukaLBR4Controller.dll")]
    [DeploymentItem("Microsoft.Robotics.Manipulation.ControllerStateMachine.dll")]
    [DeploymentItem("Test.Robotics.Hardware.KukaLBR.KukaLBR4NativeTestLib.dll")]
    [DeploymentItem("FRI_Remote.lib")]
    [TestClass]
    public class KukaLBR4UnitTests
    {
        /// <summary>
        /// Default size of result string
        /// </summary>
        private const int DefaultResultStringSize = 200;

        /// <summary>
        /// Delegate for native test functions
        /// </summary>
        /// <param name="resultStringPointer">Point to result string.</param>
        /// <param name="bufferSize">Size of result string buffer.</param>
        public delegate void NativeTestDelegate(IntPtr resultStringPointer, int bufferSize);

        /// <summary>
        /// Executes a native test.
        /// </summary>
        /// <param name="del">Delegate to test function.</param>
        /// <param name="testName">Name of test.</param>
        public static void ExecuteNativeTest(NativeTestDelegate del, string testName)
        {
            IntPtr resultStringPointer = (IntPtr)Marshal.AllocHGlobal(DefaultResultStringSize * sizeof(char));
            del(resultStringPointer, DefaultResultStringSize);
            string resultString = Marshal.PtrToStringAnsi(resultStringPointer);
            Assert.AreEqual(TestResultStrings.TestPassString, resultString, testName + ": " + resultString);
            Marshal.FreeHGlobal(resultStringPointer);
        }

        /// <summary>
        /// Test exchange of register values using the <c>RegisterExchange</c> class. 
        /// </summary>
        [TestMethod]
        [TestCategory("Unit")]
        [Priority(0)]
        public void RegisterExchangeTest()
        {
            IntPtr resultStringPointer = (IntPtr)Marshal.AllocHGlobal(DefaultResultStringSize * sizeof(char));
            NativeRegisterExchangeTest(resultStringPointer, DefaultResultStringSize);
            string resultString = Marshal.PtrToStringAnsi(resultStringPointer);
            Assert.AreEqual(TestResultStrings.TestPassString, resultString, "RegisterExchangeTest:" + resultString);
            Marshal.FreeHGlobal(resultStringPointer);
        }

        /// <summary>
        /// Test that a hypothetical "poke" CSMD behaves as expected.
        /// </summary>
        [TestMethod]
        [TestCategory("Unit")]
        [Priority(0)]
        public void PokeTest()
        {
            IntPtr resultStringPointer = (IntPtr)Marshal.AllocHGlobal(DefaultResultStringSize * sizeof(char));
            NativePokeTest(resultStringPointer, DefaultResultStringSize);
            string resultString = Marshal.PtrToStringAnsi(resultStringPointer);
            Assert.AreEqual(TestResultStrings.TestPassString, resultString, "PokeTest:" + resultString);
            Marshal.FreeHGlobal(resultStringPointer);
        }

        /// <summary>
        /// Just ensure that we don't wind up in the weeds if many CSMDs are sent rapidly.
        /// </summary>
        [TestMethod]
        [TestCategory("Unit")]
        [Priority(0)]
        public void TestSettingCsmd()
        {
            IntPtr resultStringPointer = (IntPtr)Marshal.AllocHGlobal(DefaultResultStringSize * sizeof(char));
            NativeTestSettingCsmd(resultStringPointer, DefaultResultStringSize);
            string resultString = Marshal.PtrToStringAnsi(resultStringPointer);
            Assert.AreEqual(TestResultStrings.TestPassString, resultString, "TestSettingCsmd:" + resultString);
            Marshal.FreeHGlobal(resultStringPointer);
        }

        /// <summary>
        /// Sanity check to make sure that a trivially passing test functions properly.
        /// </summary>
        [TestMethod]
        [TestCategory("Unit")]
        [Priority(0)]
        public void NativeTestSanityCheckPassing()
        {
            IntPtr resultStringPointer = (IntPtr)Marshal.AllocHGlobal(DefaultResultStringSize * sizeof(char));
            NativeNativeSanityCheckPassing(resultStringPointer, DefaultResultStringSize);
            string resultString = Marshal.PtrToStringAnsi(resultStringPointer);
            Assert.AreEqual(TestResultStrings.TestPassString, resultString, "NativeTestSanityCheckPassing:" + resultString);
            Marshal.FreeHGlobal(resultStringPointer);
        }

        /// <summary>
        /// Sanity check to make sure that a trivially failing test function properly
        /// </summary>
        [TestMethod]
        [TestCategory("Unit")]
        [Priority(0)]
        public void NativeTestSanityCheckFailing()
        {
            IntPtr resultStringPointer = (IntPtr)Marshal.AllocHGlobal(DefaultResultStringSize * sizeof(char));
            NativeNativeSanityCheckFailing(resultStringPointer, DefaultResultStringSize);
            string resultString = Marshal.PtrToStringAnsi(resultStringPointer);
            Assert.AreEqual(TestResultStrings.FailingTestString, resultString, "NativeTestSanityCheckFailing:" + resultString);
            Marshal.FreeHGlobal(resultStringPointer);
        }

        /// <summary>
        /// Test exchange of registers
        /// </summary>
        /// <param name="resultStringPointer">String buffer to store the test result in.</param>
        /// <param name="bufferSize">Size of provided string buffer. </param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass", Justification = "The purpose of this class is to wrap PInvokes"), DllImport(
            "Test.Robotics.Hardware.KukaLBR.KukaLBR4NativeTestLib.dll",
            EntryPoint = "RegisterExchangeTest",
            CallingConvention = CallingConvention.Cdecl)]
        private static extern void NativeRegisterExchangeTest(IntPtr resultStringPointer, int bufferSize);

        /// <summary>
        /// Test that a hypothetical "poke" CSMD behaves as expected.
        /// </summary>
        /// <param name="resultStringPointer">String buffer to store the test result in.</param>
        /// <param name="bufferSize">Size of provided string buffer. </param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass", Justification = "The purpose of this class is to wrap PInvokes"), DllImport(
            "Test.Robotics.Hardware.KukaLBR.KukaLBR4NativeTestLib.dll",
            EntryPoint = "PokeTest",
            CallingConvention = CallingConvention.Cdecl)]
        private static extern void NativePokeTest(IntPtr resultStringPointer, int bufferSize);

        /// <summary>
        /// Just ensure that we don't wind up in the weeds if many CSMDs are sent rapidly.
        /// </summary>
        /// <param name="resultStringPointer">String buffer to store the test result in.</param>
        /// <param name="bufferSize">Size of provided string buffer. </param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass", Justification = "The purpose of this class is to wrap PInvokes"), DllImport(
            "Test.Robotics.Hardware.KukaLBR.KukaLBR4NativeTestLib.dll",
            EntryPoint = "TestSettingCsmd",
            CallingConvention = CallingConvention.Cdecl)]
        private static extern void NativeTestSettingCsmd(IntPtr resultStringPointer, int bufferSize);

        /// <summary>
        /// Sanity check to make sure that a trivially passing test function properly
        /// </summary>
        /// <param name="resultStringPointer">String buffer to store the test result in.</param>
        /// <param name="bufferSize">Size of provided string buffer. </param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass", Justification = "The purpose of this class is to wrap PInvokes"), DllImport(
            "Test.Robotics.Hardware.KukaLBR.KukaLBR4NativeTestLib.dll",
            EntryPoint = "NativeSanityCheckPassing",
            CallingConvention = CallingConvention.Cdecl)]
        private static extern void NativeNativeSanityCheckPassing(IntPtr resultStringPointer, int bufferSize);

        /// <summary>
        /// Sanity check to make sure that a trivially failing test function properly
        /// </summary>
        /// <param name="resultStringPointer">String buffer to store the test result in.</param>
        /// <param name="bufferSize">Size of provided string buffer. </param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1060:MovePInvokesToNativeMethodsClass", Justification = "The purpose of this class is to wrap PInvokes"), DllImport(
            "Test.Robotics.Hardware.KukaLBR.KukaLBR4NativeTestLib.dll",
            EntryPoint = "NativeSanityCheckFailing",
            CallingConvention = CallingConvention.Cdecl)]
        private static extern void NativeNativeSanityCheckFailing(IntPtr resultStringPointer, int bufferSize);
    }
}
